package lueder.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lueder.classes.*;
import lueder.db.*;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public Test() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>Test</h1>");

		out.println("SessionId: " + session.getCreationTime() + "<p>");
		out.println("Session Values: ");
		if(session.getAttributeNames().hasMoreElements())
		{
		out.println("<ul>");
		for (Enumeration<String> attributes = session.getAttributeNames(); attributes.hasMoreElements();) {
			out.println("<li>"+attributes.nextElement());
		}
		out.println("</ul>");
		}
		else
			out.println("keine vorhanden");

		Customer c;
		if (session.getAttribute("customer") == null) {
			out.println("Nicht vorhanden, Customer wird erstellt!<p>");
			c = new Customer("Lueder", "Giffey");
			session.setAttribute("customer", c);
		} else {
			out.println("Ist vorhanden:<p>");
			out.println(session.getAttribute("customer").toString() + "<p>");
		}
		out.println("<p><a href=\"./Test\">Test</a>");
		out.println("<h1>DB Test</h1>");

		out.println(DbTools
				.getTableFromDb("SELECT first_name, last_name, email, street, zipcode, city"
						+ " FROM customer"
						+ " JOIN address ON(address_id=address.id)"
						+ " JOIN zipcodes ON (postal_code_id=zipcodes.id)"));

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
